﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace IT_Pro.Forms
{
    public partial class frmNVThemNhanVien : DevExpress.XtraEditors.XtraForm
    {
        public frmNVThemNhanVien()
        {
            InitializeComponent();
        }

        private void frmNVThemNhanVien_Load(object sender, EventArgs e)
        {
            HienThidsNhanVien();
            LoadDSPhongvaoCBO();
        }

        private void HienThidsNhanVien()
        {
            gridNhanVien.DataSource = Class.NV_NhanVien.HienThiDsNhanVien();

        }
        private void LoadDSPhongvaoCBO()
        {
            cboPhongBan.Properties.DataSource = Class.NV_PhongBan.HienThiDsPhong();
            cboPhongBan.Properties.ValueMember = "MaPhong";
            cboPhongBan.Properties.DisplayMember = "TenPhong";
        }

        private void btnThemNhanVien_Click(object sender, EventArgs e)
        {
            if (!KiemtraNhaplieu())
            {
                return;
            }
            Class.NV_NhanVien nv = new Class.NV_NhanVien();
            nv.MaNV = txtMaNV.Text;
            nv.TenNV = txtHoTen.Text;
            nv.DienThoaiNB = txtDienThoaiNB.Text;
            nv.DienThoaiDD = txtDienThoaiDD.Text;
            nv.Mail = txtMail.Text;
            nv.TenPhong = cboPhongBan.EditValue.ToString();
            nv.TaiKhoan = txtTaiKhoan.Text;
            nv.MatKhau = txtMatKhau.Text;
            try
            {
                if (Class.NV_NhanVien.ThemNhanVien(nv))
                {
                    MessageBox.Show("Thêm nhân viên thành công !");
                    HienThidsNhanVien();
                }
                else
                {
                    MessageBox.Show("Mã nhân viên đã có. vui lòng chọn mã Khác");
                }
            }
            catch
            {
                MessageBox.Show("Có lỗi trong quá trình thêm nhân viên.");
            }

        }

        private bool KiemtraNhaplieu()
        {
            if (cboPhongBan.EditValue.ToString().Length < 0)
            {
                cboPhongBan.Focus();
                return false;
            }
            if (txtMaNV.Text.Length < 6)
            {
                txtMaNV.Focus();
                return false;
            }
            return true;
        }

        private void txtHoTen_Validated(object sender, EventArgs e)
        {
            string _strsuly;           
                _strsuly = txtHoTen.Text.Trim();
                _strsuly = _strsuly.ToLower();
            if (_strsuly.Length > 0)
             {
                string[] _sulychucai;
                string KQ = "";
                _sulychucai = _strsuly.Split(' ');
                for (int i = 0; i < _sulychucai.Length; i++)
                {
                    string _chucaidau = _sulychucai[i].Substring(0, 1);
                    _chucaidau = _chucaidau.ToUpper();
                    _sulychucai[i] = _chucaidau + _sulychucai[i].Substring(1, _sulychucai[i].Length - 1);

                    KQ += _sulychucai[i] + " ";
                }
                txtHoTen.Text = KQ.Trim();
                _strsuly = bodautiengviet(_strsuly);
                txtMail.Text = _strsuly + "@lbc.com.vn";
                txtTaiKhoan.Text = _strsuly;
            }
        }

        private void txtMaNV_Validated(object sender, EventArgs e)
        {
            txtMaNV.Text = txtMaNV.Text.ToUpper();
        }

        private string bodautiengviet(string chuoi)
        {
            
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {

                for (int j = 0; j < VietnameseSigns[i].Length; j++)

                    chuoi = chuoi.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);

            }
            string[] chuoi_mail = chuoi.Split(' ');
            switch (chuoi_mail.Length)
            {
                case 2:
                  chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + "@lbc.com.vn";
                    break;
                  case 3:
                  chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + "." + chuoi_mail[2].ToString();
                 break;
                  case 4:
                 chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + chuoi_mail[2].ToString() + "." + chuoi_mail[3].ToString();
                 break;
                  case 5:
                 chuoi = chuoi_mail[0].ToString() + "-" + chuoi_mail[1].ToString() + chuoi_mail[2].ToString() + chuoi_mail[3].ToString() + "." + chuoi_mail[4].ToString() ;
                 break;
             }
            return chuoi;
        }

        private static readonly string[] VietnameseSigns = new string[]

         {
        "aAeEoOuUiIdDyY",
        "áàạảãâấầậẩẫăắằặẳẵ",
        "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
        "éèẹẻẽêếềệểễ",
        "ÉÈẸẺẼÊẾỀỆỂỄ",
        "óòọỏõôốồộổỗơớờợởỡ",
        "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
        "úùụủũưứừựửữ",
        "ÚÙỤỦŨƯỨỪỰỬỮ",
        "íìịỉĩ",
        "ÍÌỊỈĨ",
        "đ",
        "Đ",
        "ýỳỵỷỹ",
        "ÝỲỴỶỸ"
         };

        private void btnXoaNhanVien_Click(object sender, EventArgs e)
        {
            int SelectedRow = gridDanhSachNhanVien.FocusedRowHandle;
            if (SelectedRow >= 0)
            {
                DataRow dr = gridDanhSachNhanVien.GetDataRow(SelectedRow);
                string _value = dr["MaNV"].ToString();
                try
                {
                    if (Class.NV_NhanVien.XoaNhanVien(_value))
                    {
                        MessageBox.Show(" Xóa thành Công !");
                        HienThidsNhanVien();
                    }
                    else
                    {
                        MessageBox.Show(" Xóa không thành công.");
                    }
                }
                catch
                {
                    MessageBox.Show(" Có lỗi trong quá trình xóa .");
                }
            }
            else
            {
                MessageBox.Show(" Bạn Chưa chọn Nhân viên.");
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            Forms.frmNVThemNhanVien_Import frm = new frmNVThemNhanVien_Import();
            frm.ShowDialog();
        }


    }
}